
package com.compuvest.webstat;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
// </editor-fold>

public class Amazon extends HttpServlet {

    private static final Logger logger = Logger.getLogger(Amazon.class.getName());

    private DataSource ds;

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        try {
            Context initCtx = new InitialContext();
            ds = (DataSource)initCtx.lookup("jdbc/mysql");
        }
        catch(Exception e) {
            logger.log(Level.SEVERE, "init()", e);
        }


        logger.log(Level.INFO, "version = {0}", getVersion());
     }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest(HttpServletRequest request, HttpServletResponse response)">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        logger.log(Level.INFO, "pg={0}", request.getParameter("pg"));
        updateAmazon(request.getParameter("pg"));

    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getVersion()">

    private String getVersion() {

        String result = "Error";
        try(
            Connection conn = ds.getConnection();
            PreparedStatement ps = conn.prepareStatement("SELECT VERSION() version");
            ResultSet rs = ps.executeQuery()
         ) {
            if(rs.next()) {
                result = rs.getString("version");
            }
        }
        catch(SQLException e) {
            logger.log(Level.SEVERE, "getVersion()", e);
        }

        return result;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="updateAmazon()">

    private void updateAmazon(String page) {

        if(page == null) { return; }

        try(
            Connection conn = ds.getConnection();
            CallableStatement cs = conn.prepareCall("{ call webstat.setAmazonClicks(?) }")
        ) {
            cs.setString(1, page);

            cs.executeUpdate();
        }
        catch(SQLException e) {
            logger.log(Level.SEVERE, "updateAmazon()", e);
        }
    }
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Amazon";
    }

    @Override
    public void destroy() {
        super.destroy();

        ds = null;

    }
// </editor-fold>


}
